/**
 * 试剂瓶store
 */
import { makeAutoObservable } from "mobx";
import { ITBadResp, ITBottle } from "./interfaces";
import { checkResonse } from "../api";
import {
  fetchAddBottle,
  fetchAllBottle,
  fetchUpdateBottle,
  fetchDelBottle,
} from "../api/bottle";

export interface ITBottleStore {
  // 显示新增编辑弹窗
  showAddEditBottleModal: boolean;

  // 修改显示新增编辑弹窗
  changeShowAddEditBottleModal: (data: boolean) => void;

  // 新增编辑试剂请求状态
  addEditBottleLoading: boolean;

  // 修改新增编辑试剂请求状态
  changeAddEditBottleLoading: (data: boolean) => void;

  // 新增试剂瓶
  addBottle: (data: any) => Promise<ITBadResp | null>;

  // 编辑试剂瓶
  updateBottle: (data: any) => Promise<ITBadResp | null>;

  // 当前编辑的试剂瓶数据
  editBottleData: ITBottle | null;

  // 修改当前编辑的试剂瓶数据
  changeEditBottleData: (data: ITBottle | null) => void;

  // 所有试剂瓶
  allBottle: ITBottle[];

  // 修改所有试剂瓶
  changeAllBottle: (data: ITBottle[]) => void;

  // 请求所有试剂瓶状态
  allLoading: boolean;

  // 修改请求所有试剂瓶状态
  changeAllLoading: (data: boolean) => void;

  // 请求所有试剂瓶
  getAllBottle: () => Promise<ITBadResp | void>;

  // 删除试剂瓶状态
  delBottleLoading: boolean;

  // 修改删除试剂瓶状态
  changeDelBottleLoading: (data: boolean) => void;

  // 删除试剂瓶
  delteBottle: (data: any) => Promise<ITBadResp | null>;
}

class BottleStore {
  constructor() {
    makeAutoObservable(this, {}, { autoBind: true });
  }

  // 显示新增编辑弹窗
  showAddEditBottleModal = false;

  // 修改显示新增编辑弹窗
  changeShowAddEditBottleModal = (data: boolean) =>
    (this.showAddEditBottleModal = data);

  // 新增编辑试剂请求状态
  addEditBottleLoading = false;

  // 修改新增编辑试剂请求状态
  changeAddEditBottleLoading = (data: boolean) =>
    (this.addEditBottleLoading = data);

  // 新增试剂瓶
  addBottle = async (data: any): Promise<ITBadResp | null> => {
    this.changeAddEditBottleLoading(true);
    const res = await fetchAddBottle(data);
    this.changeAddEditBottleLoading(false);
    if (checkResonse(res)) {
      return null;
    }
    const { message } = res.data;
    return {
      msg: message,
    };
  };

  // 编辑试剂瓶
  updateBottle = async (data: any): Promise<ITBadResp | null> => {
    this.changeAddEditBottleLoading(true);
    const res = await fetchUpdateBottle(data);
    this.changeAddEditBottleLoading(false);
    if (checkResonse(res)) {
      return null;
    }
    const { message } = res.data;
    return {
      msg: message,
    };
  };

  // 当前编辑的试剂瓶数据
  editBottleData: ITBottle | null = null;

  // 修改当前编辑的试剂瓶数据
  changeEditBottleData = (data: ITBottle | null) =>
    (this.editBottleData = data);

  // 所有试剂瓶
  allBottle: ITBottle[] = [];

  // 修改所有试剂瓶
  changeAllBottle = (data: ITBottle[]) => (this.allBottle = data);

  // 请求所有试剂瓶状态
  allLoading = false;

  // 修改请求所有试剂瓶状态
  changeAllLoading = (data: boolean) => (this.allLoading = data);

  // 请求所有试剂瓶
  getAllBottle = async (): Promise<ITBadResp | void> => {
    this.changeAllLoading(true);
    const res = await fetchAllBottle();
    this.changeAllLoading(false);
    if (checkResonse(res)) {
      const { data } = res.data;
      this.changeAllBottle(data);
      return data;
    }
  };

  // 删除试剂瓶状态
  delBottleLoading = false;

  // 修改删除试剂瓶状态
  changeDelBottleLoading = (data: boolean) => (this.delBottleLoading = data);

  // 删除试剂瓶
  delteBottle = async (data: any): Promise<ITBadResp | null> => {
    this.changeDelBottleLoading(true);
    const res = await fetchDelBottle(data);
    this.changeDelBottleLoading(false);
    if (checkResonse(res)) {
      return null;
    }
    const { message } = res.data;
    return {
      msg: message,
    };
  };
}

const bottleStore: ITBottleStore = new BottleStore();
export default bottleStore;
